package com.kee.server.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kee.server.mapper.MenuMapper;
import com.kee.server.pojo.Menu;
import com.kee.server.service.IMenuService;
import com.kee.server.utils.AdminUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kee
 * @since 2021-03-22
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Resource
    private MenuMapper menuMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 通过用户id查询菜单列表
     *
     * @return
     */
    @Override
    public List<Menu> getMenusByAdminId() {

        Integer adminId = AdminUtils.getCurrentAdmin().getId();
        List<Menu> menus = null;

        try{
            ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
            menus = (List<Menu>) valueOperations.get("menu_" + adminId);
            if (CollectionUtils.isEmpty(menus)) {
                menus = menuMapper.getMenusByAdminId(adminId);
                System.out.println(menus);
                valueOperations.set("menu_" + adminId, menus);
            }
        }catch (Exception e){
            menus = menuMapper.getMenusByAdminId(adminId);
            System.out.println("Redis Unable");
        }

        return menus;
    }

    /**
     * 根据角色获得菜单列表
     * @return
     */
    @Override
    public List<Menu> getMenusWithRole() {
        return menuMapper.getMenusWithRole();
    }

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }

    @Override
    public List<Menu> getAllMenusByRid(Integer rid) {
        return menuMapper.getAllMenusByRid(rid);
    }

    @Override
    public List<Integer> getMidsByRid(Integer rid) {
        return null;
    }

    @Override
    public boolean updateMenuRole(Integer rid, Integer[] mids) {
        return false;
    }
}
